from exit import db
from datetime import datetime, date
from werkzeug.security import generate_password_hash, check_password_hash


class CMSUser(db.Model):
    __tablename__ = 'c_cms_user'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(50))
    _password = db.Column(db.String(100))
    telephone = db.Column(db.String(50), unique=True)
    create_time = db.Column(db.DateTime, default=datetime.now)
    permission = db.Column(db.Integer, default=0)
    cms_info = db.Column(db.String(32))
    logo_big = db.Column(db.String(100))
    logo_small = db.Column(db.String(100))
    power = db.Column(db.String(100), default='')
    token = db.Column(db.String(32))
    error_num = db.Column(db.Integer, default=0)
    store_id = db.Column(db.Integer)

    @property
    def password(self):
        return self._password

    @password.setter
    def password(self, raw_password):
        self._password = generate_password_hash(raw_password)

    def check_password(self, raw_password):
        result = check_password_hash(self.password, raw_password)
        return result

    def cms_user_json(self):
        return {
            'id': self.id,
            'username': self.username,
            'telephone': self.telephone,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
        }


class CMSRecord(db.Model):
    __tablename__ = 'c_cms_record'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    cms_id = db.Column(db.Integer)
    user_id = db.Column(db.String(100))
    record = db.Column(db.String(255))
    create_time = db.Column(db.DateTime, default=datetime.now)
    ip = db.Column(db.String(32))
    r_label = db.Column(db.Integer)
    r_state = db.Column(db.Integer)

    def cms_record_json(self):
        return {
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
            'record': self.record,
            'ip': self.ip,
        }


class CMSRole(db.Model):    # 后台角色权限
    __tablename__ = 'c_cms_role'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    cms_id = db.Column(db.Integer)
    menu_id = db.Column(db.Integer)     # 菜单id
    menu_name = db.Column(db.String(100))     # 菜单名称
    status = db.Column(db.Integer, default=0)   # 状态
    perms = db.Column(db.Integer, default=0)   # 读写权限
    is_show = db.Column(db.Integer, default=0)  # 展示权限

    create_time = db.Column(db.DateTime, default=datetime.now)

    def cms_role_json(self):
        return {
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
        }


class CMSMenu(db.Model):    # 后台菜单
    __tablename__ = 'c_cms_menu'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    menu_name = db.Column(db.String(100))     # 菜单名称
    p_id = db.Column(db.Integer)
    menu_type = db.Column(db.Integer)   # 菜单类型 1,目录 2,菜单 3,按钮,
    url = db.Column(db.String(100))     # 链接
    status = db.Column(db.Integer, default=0)   # 状态

    create_time = db.Column(db.DateTime, default=datetime.now)

    def cms_menus_json(self):
        return {
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S'),
        }

